package com.itheima.service.impl;

import com.itheima.dto.HotSetmealDTO;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.HotSetmeal;
import com.itheima.pojo.Order;
import com.itheima.service.ReportService;
import com.itheima.vo.BusinessReportDataVO;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 获取运营统计数据
     * @return
     */
    @Override
    public BusinessReportDataVO getBusinessReportData() {
        BusinessReportDataVO businessReportDataVO = new BusinessReportDataVO();
        //获取当天日期
        LocalDate today = LocalDate.now();
        //今日新增会员
        Integer todayNewMember = memberMapper.findMemberCountByToday(today);
        //总会员
        Integer allMember = memberMapper.findAllMember();
        //本周新增会员
        Integer thisWeekNewMember = memberMapper.findThisWeekNewMember();
        //本月新增会员
        Integer thisMonthNewMember = memberMapper.findThisMonthNewMember();
        //今日预约数
        Integer todayOrderNumber = orderMapper.findTodayOrderNumber();
        //今日到诊数
        Integer todayVisitsNumber = orderMapper.findTodayVisitsNumber(Order.ORDERSTATUS_YES);
        //本周预约数
        Integer thisWeekOrderNumber = orderMapper.findThisWeekOrderNumber();
        //本周到诊数
        Integer thisWeekVisitsNumber = orderMapper.findThisWeekVisitsNumber(Order.ORDERSTATUS_YES);
        //本月预约数
        Integer thisMonthOrderNumber = orderMapper.findThisMonthOrderNumber();
        //本月到诊数
        Integer thisMonthVisitsNumber = orderMapper.findThisMonthVisitsNumber(Order.ORDERSTATUS_YES);
        //热门套餐
        //处理setmeal_count无法查到值只能setmealCount（由于开启spring映射）问题
        List<HotSetmealDTO> hotSetmealDTOS = setmealMapper.findHotSetmeal();
        List<HotSetmeal> hotSetmealList =new ArrayList<>();
        for (HotSetmealDTO hotSetmealDTO : hotSetmealDTOS) {
              HotSetmeal hotSetmeal = new HotSetmeal();
              hotSetmeal.setName(hotSetmealDTO.getName());
              hotSetmeal.setProportion(hotSetmealDTO.getProportion());
              hotSetmeal.setSetmeal_count(hotSetmealDTO.getSetmealCount());
              hotSetmealList.add(hotSetmeal);
        }
        businessReportDataVO.setTodayNewMember(todayNewMember);
        businessReportDataVO.setTotalMember(allMember);
        businessReportDataVO.setThisWeekNewMember(thisWeekNewMember);
        businessReportDataVO.setThisMonthNewMember(thisMonthNewMember);
        businessReportDataVO.setTodayOrderNumber(todayOrderNumber);
        businessReportDataVO.setTodayVisitsNumber(todayVisitsNumber);
        businessReportDataVO.setThisWeekOrderNumber(thisWeekOrderNumber);
        businessReportDataVO.setThisWeekVisitsNumber(thisWeekVisitsNumber);
        businessReportDataVO.setThisMonthOrderNumber(thisMonthOrderNumber);
        businessReportDataVO.setThisMonthVisitsNumber(thisMonthVisitsNumber);
        businessReportDataVO.setHotSetmeal(hotSetmealList);
        businessReportDataVO.setReportDate(today);
        return businessReportDataVO;
    }

    /**
     * 数据导出
     * @param response
     */
    @Override
    public void exportBusinessReport(HttpServletResponse response) {
        response.setContentType("application/vnd.ms-excel");
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("templates/report_template.xlsx");
        BusinessReportDataVO businessReportData = getBusinessReportData();
        //  基于模板文件创建一个新的Excel文件
        XSSFWorkbook excel = null;
        try {
            excel = new XSSFWorkbook(inputStream);
            // 获取表格文件的sheet页
            XSSFSheet sheet = excel.getSheet("Sheet1");
            XSSFRow row = sheet.getRow(2);
            // 日期
            row.getCell(5).setCellValue(businessReportData.getReportDate().toString());
            // 获取sheet的第5行
            row = sheet.getRow(4);
            // 今日新增会员数
            row.getCell(5).setCellValue(businessReportData.getTodayNewMember());
            // 总会员数
            row.getCell(7).setCellValue(businessReportData.getTotalMember());
            // 获取sheet的第6行
            row = sheet.getRow(5);
            // 本周新增会员数
            row.getCell(5).setCellValue(businessReportData.getThisWeekNewMember());
            // 本月新增会员数
            row.getCell(7).setCellValue(businessReportData.getThisMonthNewMember());
            // 获取sheet的第8行
            row = sheet.getRow(7);
            // 今日预约数
            row.getCell(5).setCellValue(businessReportData.getTodayOrderNumber());
            // 今日到诊数
            row.getCell(7).setCellValue(businessReportData.getTodayVisitsNumber());
            // 获取sheet的第9行
            row = sheet.getRow(8);
            // 本周预约数
            row.getCell(5).setCellValue(businessReportData.getThisWeekOrderNumber());
            // 本周到诊数
            row.getCell(7).setCellValue(businessReportData.getThisWeekVisitsNumber());
            // 获取sheet的第10行
            row = sheet.getRow(9);
            // 本月预约数
            row.getCell(5).setCellValue(businessReportData.getThisMonthOrderNumber());
            // 本月到诊数
            row.getCell(7).setCellValue(businessReportData.getThisMonthVisitsNumber());
            // 热门套餐数据
            List<HotSetmeal> hotSetmeal = businessReportData.getHotSetmeal();
            System.out.println(hotSetmeal.get(0));
            // 获取sheet的第13到16行
            for (int i = 12; i < 16; i++) {
                row = sheet.getRow(i);
                // 套餐名称
                row.getCell(4).setCellValue(hotSetmeal.get(i - 12).getName());
                // 预约数量
                row.getCell(5).setCellValue(hotSetmeal.get(i - 12).getSetmeal_count());
                // 占比率
                row.getCell(6).setCellValue(hotSetmeal.get(i - 12).getProportion());
            }

            ServletOutputStream servletOutputStream = response.getOutputStream();
            excel.write(servletOutputStream);
           //TODO inputStream为什么只能在这关
            servletOutputStream.close();
            excel.close();
            inputStream.close();

        } catch (IOException e) {
            throw new RuntimeException("文件上传失败");
        }
    }
}
